package com.remiss.seckilldemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.remiss.seckilldemo.exception.GlobalExecption;
import com.remiss.seckilldemo.mapper.UserMapper;
import com.remiss.seckilldemo.pojo.User;
import com.remiss.seckilldemo.service.IUserService;
import com.remiss.seckilldemo.utils.CookieUtil;
import com.remiss.seckilldemo.utils.UUIDUtil;
import com.remiss.seckilldemo.vo.LoginVO;
import com.remiss.seckilldemo.vo.ResposeBean;
import com.remiss.seckilldemo.vo.ResposeBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author remiss
 * @since 2021-03-20
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResposeBean doLogin(LoginVO loginVO, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
//        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
//            return ResposeBean.error(ResposeBeanEnum.LOGIN_ERROR);
//        }
//        if(ValidatorUtil.isMobile(mobile)){
//            return  ResposeBean.error(ResposeBeanEnum.MOBILE_ERROR);
//        }
        User user = userMapper.selectById(mobile);
        if(user == null){
            throw new GlobalExecption(ResposeBeanEnum.LOGIN_ERROR);
        }else if(!user.getPassword().equals(password)){
            throw new GlobalExecption(ResposeBeanEnum.PASSWORD_ERROR);
        }
        //生成cookie
        String ticket = UUIDUtil.uuid();
//        将用户信息存入redis中
        redisTemplate.opsForValue().set("user: "+ticket ,user);


//        request.getSession().setAttribute(ticket,user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return ResposeBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if(StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user: " + userTicket);
        if(user!=null){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        log.warn("是否获取到user: "+String.valueOf(user!=null));
        return user;
    }

    @Override
    public ResposeBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {
        User user= getUserByCookie(userTicket,request,response);
        if(user == null){
            throw  new GlobalExecption(ResposeBeanEnum.MOBILE_NOT_EXIT);
        }
        user.setPassword(password);
        int result = userMapper.updateById(user);
        if(1==result){
            redisTemplate.delete("user: "+userTicket);
            return ResposeBean.success();
        }
        return ResposeBean.error(ResposeBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}
